package com.zw.a.basic.a10.demo05_arraylist;

import com.itheima.demo01_collection.Student;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

//案例: ArrayList集合存储学生对象并遍历.
public class Demo02 {
    public static void main(String[] args) {
        //1.定义学生类, 属性为姓名和年龄.
        //2.创建ArrayList集合, 用来存储学生信息.
        ArrayList<Student> list = new ArrayList<>();
        //3.往集合中添加3个学生对象.
        list.add(new Student("刘亦菲", 33));
        list.add(new Student("赵丽颖", 31));
        list.add(new Student("高圆圆", 35));
        //4.通过三种方式, 遍历上述的集合.
        //方式一: 迭代器
        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s);
        }
        System.out.println("----------------------");

        //方式二: 普通for
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s);
        }
        System.out.println("-------------------------");

        //方式三: 增强for, 掌握.
        for (Student s : list) {
            System.out.println(s);
        }
        System.out.println("-------------------------");

        //方式四: 列表迭代器, 掌握.
        ListIterator<Student> lit = list.listIterator();
        while (lit.hasNext()) {
            Student s = lit.next();
            System.out.println(s);
        }
        System.out.println("-------------------------");

        //方式五: 把集合转成数组, 然后遍历.  Collection#toArray();    了解.
        Object[] objs = list.toArray();
        for (Object obj : objs) {
            System.out.println(obj);
        }
    }
}
